package com.py.credit.controller;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.AuthorizeRecord;
import com.py.credit.service.AuthorizeRecordService;
import com.py.credit.service.XypjRemoteBaseService;
import com.py.credit.utils.UuidUtil;
import com.py.framework.core.common.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @author YaoS
 * @date 2019-05-13 15:00
 */
@RestController
@RequestMapping("/authorizeRecord")
public class AuthorizeRecordController {

    @Autowired
    XypjRemoteBaseService xypjRemoteBaseService;

    @Autowired
    AuthorizeRecordService recordService;

    /**
     * 功能描述:申请对象信息
     *
     * @param record 对象信息
     * @return 新增结果
     * @author YaoS
     * @date 19/5/13 14:59
     */
    @RequestMapping(value = "/addRecord.json")
    public RestServiceResult<String> addRecord(AuthorizeRecord record) {
        RestServiceResult result=new RestServiceResult<>();
        record.setToken(UuidUtil.getUUid());
        try {
            recordService.addRecord(record);
        }catch (Exception e){
            result.setMsg("部门重复！");
            result.setCode(RestServiceResult.CODE_UNKNOW);
        }
        return result;
    }

    /**
     * 功能描述:分页查询申请对象信息
     *
     * @param deptName 部门名称
     * @param page     分页信息
     * @return 新增结果
     * @author YaoS
     * @date 19/5/13 14:59
     */
    @RequestMapping(value = "/findRecordListForPage.json")
    public RestServiceResult<List> selectRecordListForPage(
            @RequestParam("deptName") String deptName, Page page) {
        List<AuthorizeRecord> list = recordService.selectRecordListForPage(deptName, page);
        return new RestServiceResult<>(page.getTotalCount(), list);
    }

    /**
     * 功能描述:查询所有机构信息
     *
     * @return 查询结果
     * @author YaoS
     * @date 19/5/13 14:59
     */
    @RequestMapping(value = "/findAllRecordList.json")
    public RestServiceResult<List> selectAllRecordList(String modelId) {
        List<AuthorizeRecord> list = recordService.selectAllRecordList(modelId);
        return new RestServiceResult<>(list);
    }

    /**
     * 功能描述: 根据id查询详情
     *
     * @param id id
     * @return AuthorizeRecord 详情
     * @author YaoS
     * @date 19/5/15 17:02
     */
    @RequestMapping(value = "/findRecordDetail.json")
    public RestServiceResult findRecordDetail(String id) {
        AuthorizeRecord recordDetail = recordService.selectRecordDetail(id);
        return new RestServiceResult(recordDetail);
    }


    /**
     * 功能描述:更新对象信息
     *
     * @param record 对象信息
     * @return 新增结果
     * @author YaoS
     * @date 19/5/13 14:59
     */
    @RequestMapping(value = "/updateRecord.json")
    public RestServiceResult<String> updateRecord(AuthorizeRecord record) {
        recordService.updateRecord(record);
        return new RestServiceResult<>();
    }

    /**
     * 功能描述: 根据id批量删除机构记录
     *
     * @param ids 企业id
     * @return 删除结果
     * @author YaoS
     * @date 19/5/14 16:49
     */
    @RequestMapping(value = "/deleteRecord.json")
    public RestServiceResult<String> deleteRecord(String ids) {
        recordService.deleteRecordByIds(Arrays.asList(ids.split(",")));
        return new RestServiceResult<>();
    }

}
